﻿/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

package com.chasekernan.hxnova.componenteditor;

import com.chasekernan.hxnova.core.components.ComponentSet;
import flash.events.Event;
import org.aswing.border.EmptyBorder;
import org.aswing.GridLayout;
import org.aswing.Insets;
import org.aswing.JButton;
import org.aswing.JComboBox;
import org.aswing.JFrame;
import org.aswing.JLabel;
import org.aswing.JOptionPane;
import org.aswing.JPanel;
import org.aswing.geom.IntDimension;
import org.aswing.JTextField;

class NewComponentWindow extends JFrame {
    
    public static var OK : String = "ok";
    public static var CANCEL : String = "cancel";
    
    private var types : JComboBox;
    private var nameInput : JTextField;
    private var okButton : JButton;
    private var cancelButton : JButton;
    
    public function new(?parent : Dynamic) {
        super(parent, "New Component", true);
        
        setContentPane(setupContents());
        setSize(new IntDimension(350, 125));
        setPreferredSize(new IntDimension(350, 130));
        setLocationXY(100, 225);
        pack();
        
        okButton.addActionListener(onOK);
        cancelButton.addActionListener(onCancel);
    }
    
    private function setupContents() : JPanel {
        var contents : JPanel = new JPanel(new GridLayout(3, 2, 5, 5));
        contents.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        
        contents.append(ComponentEditorPanel.setupDefaultLabel("Type"));
        types = new JComboBox(ComponentEditorPanel.getComponentClassNames());
        types.setSelectedIndex(0);
        types.setMaximumRowCount(7);
        types.getPopupList().updateListView();
        contents.append(types);
               
        contents.append(ComponentEditorPanel.setupDefaultLabel("Name"));
        nameInput = new JTextField("Name");
        nameInput.setMaxChars(30);
        contents.append(nameInput);
        
        okButton = new JButton("OK");
        contents.append(okButton);
        cancelButton = new JButton("Cancel");
        contents.append(cancelButton);
        
        return contents;
    }
    
    public function getComponentName() : String {
        return nameInput.getText();
    }
    
    public function getComponentType() : String {
        return untyped types.getSelectedItem();
    }
    
    private function onOK(?e : Dynamic) {
        if(ComponentEditorPanel.checkName(getComponentName())) {
            dispatchEvent(new Event(OK));
        } else {
            JOptionPane.showMessageDialog("Error", "This name is already in use, please choose another.");
        }
    }
    
    private function onCancel(?e : Dynamic) {
        dispatchEvent(new Event(CANCEL));
    }
    
}